<?php
$edit_btn = BUtils::T('edit_btn');
$preview_btn = BUtils::T('preview_btn');
$delete_btn = BUtils::T('delete_btn');
$publish_btn = BUtils::T('publish_btn');

//pop up a report for this special.
$view_report_btn = BUtils::T('view_reports_btn');

$predefined_days = BUtils::WeekDayKeys(7);


echo '<div style="text-align:right; padding:10px; border-bottom:1px solid #eee;">';
$this->widget('CLinkPager', array(
	'currentPage' => $pages->getCurrentPage(),
	'itemCount' => $pages->getItemCount(),
	'pageSize' => $pages->getPageSize(),
	'prevPageLabel' => '&lt;',
	'nextPageLabel' => '&gt;',
	'firstPageLabel' => '&lt;&lt;',
	'lastPageLabel' => '&gt;&gt;',
	'maxButtonCount' => 5,
	'header' => '',
	'htmlOptions' => array('class'=>'pagination'),
));
echo '</div>';
echo '<table class="va-top">';
foreach ($model as $k => $row) {
	$url_picture = empty($row['url_picture']) ? (empty($row['url_logo']) ? Yii::app()->baseUrl.'/images/logo_default.png' : $row['url_logo']) : $row['url_picture'];
	
	if ($row['status'] == 1) {
		$publish_flag = BToday::getSpentTime($row['uts_published_time']);
	} else {
		$publish_flag = BToday::getSpentTime($row['uts_published_time']);
	}
	
	// valid time
	$selected_hour_range = BUtils::GetHourRangeFormat($row['start_hour'], $row['end_hour']);
	
	// week days
	$selected_week_days_html = SpecialForm::HtmlWeekdays($row['week_days'], $predefined_days);
	
	//valida date
	$selected_valid_date_html = BUtils::GetDateRangeString($row['uts_start_date'], $row['uts_end_date'], 'd.m');
	
	//discount
	$discount_text = null;
	if (!empty($row['discount'])) {
		$discount_text = '-' . round($row['discount']) . '%';
	}
	if ($row['uts_end_date'] < time()) $discount_class = 'expired';
	else if ($row['uts_start_date'] > time()) $discount_class = 'upcoming';
	else $discount_class = '';
	
	//content
	$content_text = strip_tags($row['content']);
	
	//title
	$title_text = BUtils::T('special_auto_title_lbl', array(
		'{merchant_name}' => Yii::app()->user->getState('name'),
		'{price}' => Yii::app()->locale->numberFormatter->formatCurrency($row['price'], 'EUR'),
		'{value}' => Yii::app()->locale->numberFormatter->formatCurrency($row['value'], 'EUR'),
	));
?>
<tr>
	<td class="discount-field">
		<div class="discount-box <?php echo $discount_class; ?>">
			<span class="special-discount"><?php echo $discount_text; ?></span>
		</div>
	</td>
	<td class="title-field">
		<div class="title"><?php echo $title_text; ?></div>
		<div class="address"><?php echo $content_text; ?></div>
		<div class="buttons">
<?php
echo CHtml::link("<span>{$preview_btn}</span>", Yii::app()->createUrl('/special/preview', array('id'=>$row['id'])), array('class'=>'button button-small button-thin popup-btn'));
echo ' ';
if (empty($row['status'])) {
	echo CHtml::link("<span>{$edit_btn}</span>", Yii::app()->createUrl('/special/form', array('id'=>$row['id'])), array('class'=>'button button-small button-thin unpub'));
	echo ' ';
	echo CHtml::link("<span>{$delete_btn}</span>", Yii::app()->createUrl('/special/delete', array('id'=>$row['id'])), array('class'=>'button button-small button-thin delete-btn unpub'));
	echo ' ';
	echo CHtml::link("<span>{$publish_btn}</span>", Yii::app()->createUrl('/special/publish', array('id'=>$row['id'])), array('class'=>'button button-small button-thin button-compliment publish-btn unpub', 'title'=>BUtils::T('special_publish_eg')));
	echo ' ';
} elseif ($row['status'] == 1) {
	echo ' ';
	echo CHtml::link("<span>{$delete_btn}</span>", Yii::app()->createUrl('/special/delete', array('id'=>$row['id'])), array('class'=>'button button-small button-thin delete-btn unpub'));
	
	echo ' ';
	echo CHtml::link("<span>{$view_report_btn}</span>", Yii::app()->createUrl('/special/report', array('id'=>$row['id'])), array('class'=>'button button-small button-thin button-compliment popup-btn', 'title'=>BUtils::T('special_report_eg')));
	
	//the create new special from this offer
	if ($discount_class == 'expired') {
		$create_special_btn = BUtils::T('create_special_lbl');
		echo ' ';
		echo CHtml::link("<span>{$create_special_btn}</span>", Yii::app()->createUrl('/special/form', array('sid'=>$row['id'], 'view'=>($row['type']==2 ? 'instant':''))), array('class'=>'button button-small button-thin button-confirm'));
	}
}
?>
		</div>
	</td>
	<td class="period-field">
		<div class="time-range-bar <?php echo $discount_class; ?>">
			<div class="time-range"><?php echo $selected_hour_range; ?></div>
		</div>
		<div class="week-range-bar">
			<?php echo $selected_week_days_html; ?>
		</div>
		<div class="date-range-bar">
			<?php echo $selected_valid_date_html; ?>
		</div>
	</td>
</tr>
<?php
}
echo '</table>';
?>
<script type="text/javascript">
$(function() {
	$('[rel=tipsy]').tipsy({live: true, gravity:'w'});
	
	$('.pagination a').click(function(){
		$.ajax({
			'beforeSend':beforeSend, 'success':success,
			'type':'POST', 'url':$(this).attr('href'), 'data':$('#search_form').serialize()
		});
		return false;
	});
	
	$('.popup-btn').facebox({
		loadingImage: '<?php echo Yii::app()->baseUrl ?>/images/loader-bar.gif',
		closeImage: '<?php echo Yii::app()->baseUrl ?>/images/btn-close.png',
		overlayClose:false,
		width: 360
	});
	$('.delete-btn').click(function() {
		if (confirm('<?php echo BUtils::T('delete_btn_confirm'); ?>')) {
			var _this = $(this);
			if (_this.attr('disabled')) {
				return false;
			}
			jQuery.ajax({
				'type':'GET',
				'url':_this.attr('href'),
				'beforeSend':function() {
					_this.attr('disabled','disabled').addClass('button-disabled');
				},
				'success':function(html) {
					_this.removeAttr('disabled').removeClass('button-disabled');
					_this.parent().parent().parent().fadeOut('slow');
				}
			});
		}
		return false;
	});
	$('.publish-btn').click(function() {
		if (confirm('<?php echo BUtils::T('publish_btn_confirm'); ?>')) {
			var _this = $(this);
			if (_this.attr('disabled')) {
				return false;
			}
			jQuery.ajax({
				'type':'GET',
				'url':_this.attr('href'),
				'beforeSend':function() {
					_this.attr('disabled','disabled').addClass('button-disabled');
				},
				'success':function(html) {
					_this.removeAttr('disabled').removeClass('button-disabled');
					_this.parent().children('.unpub').hide();
				}
			});
		}
		return false;
	});
});
</script>